package com.armani.controller;

import com.alibaba.fastjson.JSON;
import com.armani.entity.Great;
import com.armani.model.Result;
import com.armani.service.GreatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * @ProjectName: armani
 * @Package: com.armani.controller
 * @ClassName: GreatController
 * @Description: TODO
 * @Author: Xinyibuzai
 * @Date: 2021/11/28 17:12
 * @Version: 1.0
 **/
@Controller
@RequestMapping(value = "/great", produces = "application/json; charset=utf-8")
public class GreatController {
    @Autowired
    private GreatService greatService;

    @GetMapping(value = "search.do")
    @ResponseBody
    private String listSearch(@RequestParam Integer page,
                              @RequestParam Integer limit,
                              @RequestParam(required = false) String name){
        Integer startRow = (page - 1) * limit;

        List<Great> greats = greatService.listSearch(startRow, limit, name);

        Integer count = greatService.getCount(name);

        Result result = new Result();
        result.setData(greats);
        result.setCount(count);
        result.setCode(0);
        String s = JSON.toJSONString(result);
        return s;
    }
    @RequestMapping(value = "update.do")
    @ResponseBody
    public String update(@RequestParam String name,
                         @RequestParam String image,
                         @RequestParam String url,
                         @RequestParam Integer spuId,
                         @RequestParam Integer state,
                         @RequestParam Integer sort,
                         @RequestParam(required = false) Integer id) throws ParseException {
        greatService.update(name, image, url, spuId, state, sort,id);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("修改成功！");
        String st = JSON.toJSONString(result);
        return st;
    }
    @RequestMapping(value = "insert.do")
    @ResponseBody
    public String insert(@RequestParam String name,
                         @RequestParam String image,
                         @RequestParam String url,
                         @RequestParam Integer spuId,
                         @RequestParam Integer state,
                         @RequestParam Integer sort)throws ParseException{
        greatService.insert(name, image, url, spuId, state, sort);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("添加成功！");
        String st = JSON.toJSONString(result);
        return st;
    }
    @RequestMapping(value = "delete.do")
    @ResponseBody
    public String delete(@RequestParam Integer id){
        greatService.delete(id);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("删除成功！");
        String str = JSON.toJSONString(result);
        return str;
    }
    @RequestMapping(value = "state.do")
    @ResponseBody
    public String state(@RequestParam(required = false) Integer id,
                        @RequestParam(required = false) Integer state){
        greatService.state(id, state);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("修改成功！");
        String str = JSON.toJSONString(result);
        return str;
    }
    @GetMapping(value = "page.do")
    @ResponseBody
    private String pageSearch(@RequestParam(required = false) String name){
        List<Great> greats = greatService.pageSearch(name);
        Integer count = greatService.getCount(name);
        Result result = new Result();
        result.setData(greats);
        result.setCount(count);
        result.setCode(0);
        String s = JSON.toJSONString(result);
        return s;
    }
}
